﻿///////////////////////////////////////////////////////////////////////////
// 文件名:     progresslistwidget.cpp
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-09 00:49
///////////////////////////////////////////////////////////////////////////
#include "progresslistwidget.h"
#include "progresssavedialog.h"
#include "progresswidget.h"
#include "ui_progresslistwidget.h"
#include <QDebug>
#include <QFile>
#include <QMenu>

ProgressListWidget::ProgressListWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ProgressListWidget)
{
    ui->setupUi(this);
    flowLayout = new QFlowLayout(5, 5, 5);
    ui->centerWidget->setLayout(flowLayout);
}

ProgressListWidget::~ProgressListWidget()
{
    delete flowLayout;
    delete ui;
}

void ProgressListWidget::slot_progressInfoList(QList<ProgressInfo> progressInfoList)
{
    for (ProgressInfo &progressInfo : progressInfoList)
    {
        bool exist = false;
        for (int i = 0; i < flowLayout->count(); i++)
        {
            ProgressWidget *progressWidget = (ProgressWidget *)flowLayout->itemAt(i)->widget();
            if (progressWidget->getProgressInfo().getProgress().getId() == progressInfo.getProgress().getId())
            {
                progressWidget->setProgressInfo(progressInfo);
                exist = true;
                break;
            }
        }
        if (!exist)
        {
            ProgressWidget *progressWidget = new ProgressWidget(progressInfo);
            connect(progressWidget, &ProgressWidget::signal_editProgress, this, &ProgressListWidget::slot_editProgress);
            flowLayout->addWidget(progressWidget);
        }
    }

    //若数据库删除了某个进程，UI上同步删除
    std::vector<QWidget *> toDel;
    for (int i = 0; i < flowLayout->count(); i++)
    {
        bool exist = false;
        ProgressWidget *progressWidget = (ProgressWidget *)flowLayout->itemAt(i)->widget();
        for (ProgressInfo &progressInfo : progressInfoList)
        {
            if (progressWidget->getProgressInfo().getProgress().getId() == progressInfo.getProgress().getId())
            {
                exist = true;
                break;
            }
        }

        if (!exist)
        {
            toDel.push_back(progressWidget);
        }
    }

    for (auto &it : toDel)
    {
        flowLayout->removeWidget(it);
        it->hide();
    }
}

void ProgressListWidget::slot_progressScreenshotData(uint64_t progressId, QByteArray data)
{
    for (int i = 0; i < flowLayout->count(); i++)
    {
        ProgressWidget *progressWidget = (ProgressWidget *)flowLayout->itemAt(i)->widget();
        if (progressWidget->getProgressInfo().getProgress().getId() == progressId)
        {
            progressWidget->setScreenshotData(data);
            break;
        }
    }
}
void ProgressListWidget::slot_editProgress(uint64_t connectId, ProgressInfo progressInfo)
{
    //    ProgressSaveDialog *dialog = new ProgressSaveDialog(connectId,progressInfo,this);
    //    if(dialog->exec() == QDialog::Accepted){
    //        ProgressCmdHeader progressCmdHeader;
    //        progressCmdHeader.cmdType = PROGRESS_CMD_SAVE;
    //        progressCmdHeader.progressId = progressInfo.id;
    //        signal_progressControl(connectId,progressCmdHeader,progressInfo);
    //    }
}

void ProgressListWidget::slot_addProgress(uint64_t connectId)
{
    //    ProgressInfo progressInfo;
    //    ProgressSaveDialog *dialog = new ProgressSaveDialog(connectId,progressInfo,this);
    //    if(dialog->exec() == QDialog::Accepted){
    //        ProgressCmdHeader progressCmdHeader;
    //        progressCmdHeader.cmdType = PROGRESS_CMD_SAVE;
    //        progressCmdHeader.progressId = 0;
    //        signal_progressControl(connectId,progressCmdHeader,progressInfo);
    //    }
}

void ProgressListWidget::on_scrollArea_customContextMenuRequested(const QPoint &pos)
{
    QMenu menu(this);
    if (flowLayout->count() > 0)
    {
        menu.addSeparator();
        menu.addAction(QIcon(":/res/img/png/refresh.png"), QString("刷新"), this, &ProgressListWidget::slot_removeAllProgress);
    }
    menu.exec(QCursor::pos());
}

void ProgressListWidget::slot_removeAllProgress()
{
    for (int i = 0; i < flowLayout->count();)
    {
        delete flowLayout->takeAt(i)->widget();
    }
}
